package cn.smbms.controller;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.ProviderService;
import cn.smbms.util.PageBean;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.OffsetDateTime;
import java.util.*;

/**
 * @author Tequila
 **/
@RequestMapping("/provider")
@Controller
public class ProviderController {

    @Resource
    private ProviderService providerService;

    @RequestMapping("/provideraddsave")
    public String providerAddSave(HttpServletRequest request,
                                  @ModelAttribute Provider provider,
                                  @RequestParam(value = "attachs") MultipartFile[] multipartFile){
        String idPicPath = "";

        for (int i =0;i<multipartFile.length;i++) {

            if (multipartFile[i] != null){
                // 开启文件上传操作
                // 1.定义上传路径    获取上下文的绝对路径
                String filePath = request.getSession().getServletContext().getRealPath("statics"+ File.separator+"fileUpload");
                // 2.获取原文件名
                String filename = multipartFile[i].getOriginalFilename();
                // 3.获取文件大小
                long size = multipartFile[i].getSize();
                // 4.获取后缀
                String extension = FilenameUtils.getExtension(filename);
                List<String> list = Arrays.asList("jpg","png","jpeg");
                if (!list.contains(extension.toLowerCase())){ // toLowerCase 将字符串全部变成小写
                    if (i == 0){
                        request.setAttribute("uploadFileError","对不起,暂时只支持,jpg,png,jpeg 类型的图片");
                    }else{
                        request.setAttribute("uploadOcError","对不起,暂时只支持,jpg,png,jpeg 类型的图片");

                    }
                    return "provideradd";
                }else if (size > 10485760){
                    if (i == 0){

                        request.setAttribute("uploadFileError","文件超出上传大小");
                    }else{
                        request.setAttribute("uploadOcError","文件超出上传大小");
                    }
                    return "provideradd";
                }else{
                    // 当前系统时间戳  +  随机数  + _Personal.jpg  -> uuid
                    String newFileName = UUID.randomUUID()+"_provider."+extension;
                    // 核心功能 开始文件上传
                    File file = new File(filePath,newFileName);
                    if (!file.exists()){
                        file.mkdirs();// 创建目录
                    }
                    try {
                        multipartFile[i].transferTo(file);  // 核心上传功能
                        // 保存上传地址  "statics/fileUpload/newFileName"  相对地址
                        idPicPath = "statics"+File.separator+"fileUpload"+File.separator+newFileName;
                        if (i == 0){
                            provider.setImg1(idPicPath);
                        }else{
                            provider.setImg2(idPicPath);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                        request.setAttribute("uploadFileError","文件上传异常,请重试!");
                    }
                }
            }
        }
        User userSession = (User) request.getSession().getAttribute("userSession");
        provider.setCreatedBy(userSession.getId());
        provider.setCreationDate(new Date());

        boolean result = providerService.addProvider(provider);
        if (result){
            return "redirect:/provider/providerlist";

        }else{
            return "provideradd";
        }

    }

    @RequestMapping("/provideradd")
    public String providerAdd(){
        return "provideradd";
    }

    @RequestMapping("/providerlist")
    public String providerList(HttpServletRequest request, @RequestParam(required = false) Integer pageIndex, @RequestParam(required = false) String queryProName, @RequestParam(required = false) String queryProCode){

        pageIndex = pageIndex == null ? 1 : pageIndex;
        Integer pageSize = 5;

        PageBean<Provider> page = providerService.findProviderList(pageIndex,pageSize,queryProName,queryProCode);
        request.setAttribute("page",page);
        return "providerlist";
    }

}
